package com.dy.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.dy.common.core.domain.entity.SysDept;
import com.dy.common.utils.StringUtils;
import com.dy.common.utils.poi.ExcelUtil;
import com.dy.system.domain.FreshWarningMsgTypeDetail;
import com.dy.system.domain.MedicalInstitution;
import com.dy.system.service.IFreshWarningMsgTypeDetailService;
import com.dy.system.service.IMedicalInstitutionService;
import com.dy.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dy.common.annotation.Log;
import com.dy.common.core.controller.BaseController;
import com.dy.common.core.domain.AjaxResult;
import com.dy.common.enums.BusinessType;
import com.dy.common.core.page.TableDataInfo;

/**
 * 告警消息详情Controller
 *
 * @author ruoyi
 * @date 2022-09-15
 */
@RestController
@RequestMapping("/system/warning/detail")
public class FreshWarningMsgTypeDetailController extends BaseController {
    @Autowired
    private IFreshWarningMsgTypeDetailService freshWarningMsgTypeDetailService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IMedicalInstitutionService medicalInstitutionService;

    /**
     * 查询告警消息详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FreshWarningMsgTypeDetail msgTypeDetail) {
        MedicalInstitution institution = new MedicalInstitution();
        if (StringUtils.isNotBlank(msgTypeDetail.getName())){
            institution.setName(msgTypeDetail.getName());
        }
        if (msgTypeDetail.getDeptId() != null){
            institution.setDeptId(msgTypeDetail.getDeptId());
        }
        List<MedicalInstitution> institutionList = medicalInstitutionService.selectMedicalInstitutionList(institution);
        if (CollectionUtils.isEmpty(institutionList)){
            return getDataTable(new ArrayList<>());
        }else{
            List<Long> idList = institutionList.stream().map(m -> m.getId()).collect(Collectors.toList());
            msgTypeDetail.setMedicalIdList(idList);
        }
        startPage();
        List<FreshWarningMsgTypeDetail> list = freshWarningMsgTypeDetailService.selectFreshWarningMsgTypeDetailList(msgTypeDetail);
        list.forEach(m ->{
            MedicalInstitution medicalInstitution = medicalInstitutionService.selectMedicalInstitutionById(m.getMedicalId());
            if (medicalInstitution != null){
                SysDept sysDept = sysDeptService.selectDeptById(medicalInstitution.getDeptId());
                if (sysDept != null){
                    m.setDeptName(sysDept.getDeptName());
                }
                m.setName(medicalInstitution.getName());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出告警消息详情列表
     */
    @Log(title = "告警消息详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FreshWarningMsgTypeDetail freshWarningMsgTypeDetail) {
        List<FreshWarningMsgTypeDetail> list = freshWarningMsgTypeDetailService.selectFreshWarningMsgTypeDetailList(freshWarningMsgTypeDetail);
        ExcelUtil<FreshWarningMsgTypeDetail> util = new ExcelUtil<FreshWarningMsgTypeDetail>(FreshWarningMsgTypeDetail.class);
        util.exportExcel(response, list, "告警消息详情数据");
    }

    /**
     * 获取告警消息详情详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(freshWarningMsgTypeDetailService.selectFreshWarningMsgTypeDetailById(id));
    }

    /**
     * 新增告警消息详情
     */
    @Log(title = "告警消息详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FreshWarningMsgTypeDetail freshWarningMsgTypeDetail) {
        return toAjax(freshWarningMsgTypeDetailService.insertFreshWarningMsgTypeDetail(freshWarningMsgTypeDetail));
    }

    /**
     * 修改告警消息详情
     */
    @Log(title = "告警消息详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FreshWarningMsgTypeDetail freshWarningMsgTypeDetail){
        return toAjax(freshWarningMsgTypeDetailService.updateFreshWarningMsgTypeDetail(freshWarningMsgTypeDetail));
    }

    /**
     * 删除告警消息详情
     */
    @Log(title = "告警消息详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(freshWarningMsgTypeDetailService.deleteFreshWarningMsgTypeDetailByIds(ids));
    }
}
